﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using FlexCell;
using YArchitech.Controls;
using YArchitech.ProgressBar;
using YArchitech.Revit;
using YJKMaterialStatistic;
using YJKRevitKernel.ElementSearchUtility.LinkDocumentSuite;
using YJKPresentation.Utils;

namespace YJKWndrStatisticsExcel.FastStatisticTable.Utility
{
	internal class DoorWindowGrid3
	{
        public DoorWindowGrid3(Autodesk.Revit.DB.Document doc, GridSetCommonInfo gridSetInfo, ref FlexCell.Grid grid)
		{
			this.m_grid = grid;
			this.m_doc = doc;
			this.m_gridSet = gridSetInfo;
		}

		public bool GetStatisticGrid(List<ViewPlan> lstViewPlan)
		{
			this.m_lstViewPlan = lstViewPlan;
			this.m_lstLevelValue = this.GetLevelValueName();
			if (!this.StatisticData())
			{
				return false;
			}
			this.m_grid.Locked = false;
			return this.SetDataToGrid();
		}

		public bool GetStatisticGrid(List<ViewPlan> lstViewPlan, List<StandardLevel> levels)
		{
			this.m_standardLevel = levels;
			this.m_lstViewPlan = lstViewPlan;
			this.m_lstLevelValue = this.GetStandardLevelValueName();
			if (!this.StatisticData())
			{
				return false;
			}
			this.m_grid.Locked = false;
			return this.SetDataToGrid();
		}

		private void GetStandardLevel()
		{
			foreach (ViewPlan viewPlan in this.m_lstViewPlan)
			{
				Level genLevel = viewPlan.GenLevel;
				List<ElementId> list = new List<ElementId>();
				list.Add(genLevel.Id);
				string name = viewPlan.Name;
				StandardLevel standardLevel = new StandardLevel();
				standardLevel.LevelName = name;
				standardLevel.LevelsId = list;
				standardLevel.Count = 1;
				standardLevel.StandardId = genLevel.Id;
				this.m_standardLevel.Add(standardLevel);
			}
		}

		public bool GetStatisticGrid(List<Element> lstSelElems)
		{
			if (lstSelElems == null || lstSelElems.Count <= 0)
			{
				YJKMessageBox.Show("没有需要统计的数据");
				return false;
			}
			this.m_lstSelElems = lstSelElems;
			this.m_lstViewPlan = new List<ViewPlan>();
			this.m_lstViewPlan.Add(this.m_doc.ActiveView as ViewPlan);
			this.m_lstLevelValue = this.GetLevelValueName();
			if (!this.StatisticDataInSelArea())
			{
				return false;
			}
			this.m_grid.Locked = false;
			return this.SetDataToGrid();
		}

		private bool StatisticData()
		{
			if (this.m_doc == null)
			{
				return false;
			}
			try
			{
				Dictionary<ElementId, List<string>> dictionary = new Dictionary<ElementId, List<string>>();
				this.GetAllDoorInDoc(ref dictionary);
				if (this.m_gridSet.LinkFile)
				{
					this.GetAllDoorInLinkDoc(ref dictionary);
				}
				this.SumData(ref dictionary);
				Dictionary<ElementId, List<string>> dictionary2 = new Dictionary<ElementId, List<string>>();
				this.GetAllWindowInDoc(ref dictionary2);
				if (this.m_gridSet.LinkFile)
				{
					this.GetAllWindowInLinkDoc(ref dictionary2);
				}
				this.SumData(ref dictionary2);
			}
			catch
			{
				YJKMessageBox.Show("快速统计出错");
				return false;
			}
			return true;
		}

		private bool StatisticDataInSelArea()
		{
			try
			{
				Dictionary<ElementId, List<string>> dictionary = new Dictionary<ElementId, List<string>>();
				this.GetAllDoorInSelArea(ref dictionary);
				this.SumData(ref dictionary);
				Dictionary<ElementId, List<string>> dictionary2 = new Dictionary<ElementId, List<string>>();
				this.GetAllWindowInSelArea(ref dictionary2);
				this.SumData(ref dictionary2);
			}
			catch
			{
				YJKMessageBox.Show("快速统计出错");
				return false;
			}
			return true;
		}

		private void GetAllDoorInDoc(ref Dictionary<ElementId, List<string>> dicItems)
		{
			ProgressBarForm progressBarForm = new ProgressBarForm(this.m_lstViewPlan.Count, "统计主文档中门的信息", ProgressBarStyle.Blocks, true, false);
			progressBarForm.Show(WindowHelper.HWndRevit);
			foreach (ViewPlan viewPlan in this.m_lstViewPlan)
			{
				progressBarForm.RefreshProgressBar();
				if (progressBarForm.m_stopTag)
				{
					break;
				}
				ElementId elementId = viewPlan.GetUnderLayParameter().AsElementId();
				Transaction transaction = new Transaction(this.m_doc, "ChangeBaseLine");
				transaction.Start();
				viewPlan.GetUnderLayParameter().Set(ElementId.InvalidElementId);
				transaction.Commit();
				foreach (Element element in new FilteredElementCollector(this.m_doc, viewPlan.Id).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Doors).ToElements())
				{
					FamilyInstance familyInstance = element as FamilyInstance;
					List<string> list = new List<string>();
					double elevation = element.GetYJKLevel().Elevation;
					list.Insert(0, elevation.ToString("0.00"));
					list.Insert(1, "材质门");
					string name = familyInstance.Symbol.Name;
					list.Insert(2, name);
					string item = familyInstance.Symbol.GetParameter(BuiltInParameter.CASEWORK_WIDTH).AsValueString();
					list.Insert(3, item);
					string item2 = familyInstance.Symbol.GetParameter(BuiltInParameter.FAMILY_HEIGHT_PARAM).AsValueString();
					list.Insert(4, item2);
					int i;
					for (i = 0; i < this.m_lstLevelValue.Count; i++)
					{
						if (this.m_lstLevelValue[i].Equals(elevation))
						{
							list.Insert(5 + i, "1");
						}
						else
						{
							list.Insert(5 + i, "");
						}
					}
					i += 5;
					list.Insert(i, "1");
					i++;
					list.Insert(i, "");
					i++;
					list.Insert(i, "");
					i++;
					list.Insert(i, "");
					if (!dicItems.ContainsKey(element.Id))
					{
						dicItems.Add(element.Id, list);
					}
				}
				transaction.Start();
				viewPlan.GetUnderLayParameter().Set(elementId);
				transaction.Commit();
			}
		}

		private void GetAllDoorInSelArea(ref Dictionary<ElementId, List<string>> dicItems)
		{
			ProgressBarForm progressBarForm = new ProgressBarForm(this.m_lstSelElems.Count, "统计区域中门的信息", ProgressBarStyle.Blocks, true, false);
			progressBarForm.Show(WindowHelper.HWndRevit);
			foreach (Element element in this.m_lstSelElems)
			{
				progressBarForm.RefreshProgressBar();
				if (progressBarForm.m_stopTag)
				{
					break;
				}
				if (element.Category.Id.IntegerValue == -2000023)
				{
					if (dicItems.ContainsKey(element.Id))
					{
						if (dicItems[element.Id][5] == "")
						{
							dicItems[element.Id][5] = "0";
						}
						dicItems[element.Id][5] = (Convert.ToInt32(dicItems[element.Id][5]) + 1).ToString();
					}
					else
					{
						FamilyInstance familyInstance = element as FamilyInstance;
						List<string> list = new List<string>();
						double elevation = element.GetYJKLevel().Elevation;
						list.Insert(0, elevation.ToString("0.00"));
						list.Insert(1, "材质门");
						string name = familyInstance.Symbol.Name;
						list.Insert(2, name);
						string item = familyInstance.Symbol.GetParameter(BuiltInParameter.CASEWORK_WIDTH).AsValueString();
						list.Insert(3, item);
						string item2 = familyInstance.Symbol.GetParameter(BuiltInParameter.FAMILY_HEIGHT_PARAM).AsValueString();
						list.Insert(4, item2);
						int i;
						for (i = 0; i < this.m_lstLevelValue.Count; i++)
						{
							if (this.m_lstLevelValue[i].Equals(elevation))
							{
								list.Insert(5 + i, "1");
							}
							else
							{
								list.Insert(5 + i, "");
							}
						}
						i += 5;
						list.Insert(i, "1");
						i++;
						list.Insert(i, "");
						i++;
						list.Insert(i, "");
						i++;
						list.Insert(i, "");
						if (!dicItems.ContainsKey(element.Id))
						{
							dicItems.Add(element.Id, list);
						}
					}
				}
			}
		}

		private void GetAllDoorInLinkDoc(ref Dictionary<ElementId, List<string>> dicItems)
		{
			List<RevitLinkInstance> allRevitLinkInstancesInDoc = this.GetAllRevitLinkInstancesInDoc();
			if (allRevitLinkInstancesInDoc.Count <= 0)
			{
				return;
			}
			List<int> list = new List<int>();
			List<Dictionary<ElementId, List<string>>> list2 = new List<Dictionary<ElementId, List<string>>>();
			ProgressBarForm progressBarForm = new ProgressBarForm(this.m_lstViewPlan.Count, "统计链接文档中门的信息", ProgressBarStyle.Blocks, true, false);
			progressBarForm.Show(WindowHelper.HWndRevit);
			foreach (ViewPlan viewPlan in this.m_lstViewPlan)
			{
				progressBarForm.RefreshProgressBar();
				if (progressBarForm.m_stopTag)
				{
					return;
				}
				double elevation = viewPlan.GenLevel.Elevation;
				int item = (int)(elevation * 1000.0);
				if (!list.Contains(item))
				{
					list.Add(item);
					double num = this.GetNearTopElevation(elevation);
					num -= 0.1;
					BoundingBoxUV outline = viewPlan.Outline;
					UV uv = outline.Min * (double)viewPlan.Scale;
					UV uv2 = outline.Max * (double)viewPlan.Scale;
					BoundingBoxIntersectsFilter input = new BoundingBoxIntersectsFilter(new Outline(new XYZ(uv.U, uv.V, elevation), new XYZ(uv2.U, uv2.V, num)));
					foreach (RevitLinkInstance revitLinkInstance in allRevitLinkInstancesInDoc)
					{
						Document linkDocument = revitLinkInstance.GetLinkDocument();
						if (linkDocument != null)
						{
							Dictionary<ElementId, List<string>> dictionary = new Dictionary<ElementId, List<string>>();
							try
							{
								FilteredElementCollectorLinkDocument filteredElementCollectorLinkDocument = new FilteredElementCollectorLinkDocument(linkDocument);
								filteredElementCollectorLinkDocument.AddViewLimit(input);
								foreach (BaseLinkDocumentResultBean baseLinkDocumentResultBean in filteredElementCollectorLinkDocument.OfCalss(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Doors).ToBeans())
								{
									Element useElement = baseLinkDocumentResultBean.UseElement;
									if (!dictionary.ContainsKey(useElement.Id))
									{
										FamilyInstance familyInstance = useElement as FamilyInstance;
										List<string> list3 = new List<string>();
										list3.Insert(0, elevation.ToString("0.00"));
										list3.Insert(1, "材质门");
										string name = familyInstance.Symbol.Name;
										list3.Insert(2, name);
										string item2 = familyInstance.Symbol.GetParameter(BuiltInParameter.CASEWORK_WIDTH).AsValueString();
										list3.Insert(3, item2);
										string item3 = familyInstance.Symbol.GetParameter(BuiltInParameter.FAMILY_HEIGHT_PARAM).AsValueString();
										list3.Insert(4, item3);
										int i;
										for (i = 0; i < this.m_lstLevelValue.Count; i++)
										{
											if (this.m_lstLevelValue[i].Equals(elevation))
											{
												list3.Insert(5 + i, "1");
											}
											else
											{
												list3.Insert(5 + i, "");
											}
										}
										i += 5;
										list3.Insert(i, "1");
										i++;
										list3.Insert(i, "");
										i++;
										list3.Insert(i, "");
										i++;
										list3.Insert(i, "");
										list3.Add(linkDocument.PathName);
										dictionary.Add(useElement.Id, list3);
									}
								}
							}
							catch
							{
								continue;
							}
							list2.Add(dictionary);
						}
					}
				}
			}
			this.ManagerSameIdElem(list2, ref dicItems);
		}

		private void ManagerSameIdElem(List<Dictionary<ElementId, List<string>>> lstDicItems, ref Dictionary<ElementId, List<string>> dicItems)
		{
			if (lstDicItems.Count <= 0)
			{
				return;
			}
			for (int i = 0; i < lstDicItems.Count; i++)
			{
				foreach (KeyValuePair<ElementId, List<string>> keyValuePair in lstDicItems[i])
				{
					if (dicItems.ContainsKey(keyValuePair.Key))
					{
						int num = keyValuePair.Value.Count - 1;
						List<string> list = dicItems[keyValuePair.Key];
						List<string> value = keyValuePair.Value;
						if (list.Count - 1 >= num && value.Count - 1 >= num)
						{
							string a = list[num];
							string b = value[num];
							if (a == b)
							{
								double dElevation = Convert.ToDouble(keyValuePair.Value[0]);
								int num2 = this.m_lstLevelValue.FindIndex((double elevation) => Geometry.IsEqual(elevation, dElevation, 0.01));
								if (dicItems[keyValuePair.Key][5 + num2] == "")
								{
									dicItems[keyValuePair.Key][5 + num2] = "0";
								}
								dicItems[keyValuePair.Key][5 + num2] = (Convert.ToInt32(dicItems[keyValuePair.Key][5 + num2]) + 1).ToString();
							}
						}
					}
					else
					{
						dicItems.Add(keyValuePair.Key, keyValuePair.Value);
					}
				}
			}
		}

		private void GetAllWindowInDoc(ref Dictionary<ElementId, List<string>> dicItems)
		{
			ProgressBarForm progressBarForm = new ProgressBarForm(this.m_lstViewPlan.Count, "统计主文档中窗的信息", ProgressBarStyle.Blocks, true, false);
			progressBarForm.Show(WindowHelper.HWndRevit);
			foreach (ViewPlan viewPlan in this.m_lstViewPlan)
			{
				progressBarForm.RefreshProgressBar();
				if (progressBarForm.m_stopTag)
				{
					break;
				}
				ElementId elementId = viewPlan.GetUnderLayParameter().AsElementId();
				Transaction transaction = new Transaction(this.m_doc, "ChangeBaseLine");
				transaction.Start();
				viewPlan.GetUnderLayParameter().Set(ElementId.InvalidElementId);
				transaction.Commit();
				foreach (Element element in new FilteredElementCollector(this.m_doc, viewPlan.Id).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Windows).ToElements())
				{
					FamilyInstance familyInstance = element as FamilyInstance;
					List<string> list = new List<string>();
					double elevation = element.GetYJKLevel().Elevation;
					list.Insert(0, elevation.ToString("0.00"));
					list.Insert(1, "材质窗");
					string name = familyInstance.Symbol.Name;
					list.Insert(2, name);
					string item = familyInstance.Symbol.GetParameter(BuiltInParameter.CASEWORK_WIDTH).AsValueString();
					list.Insert(3, item);
					string item2 = familyInstance.Symbol.GetParameter(BuiltInParameter.FAMILY_HEIGHT_PARAM).AsValueString();
					list.Insert(4, item2);
					int i;
					for (i = 0; i < this.m_lstLevelValue.Count; i++)
					{
						if (this.m_lstLevelValue[i].Equals(elevation))
						{
							list.Insert(5 + i, "1");
						}
						else
						{
							list.Insert(5 + i, "");
						}
					}
					i += 5;
					list.Insert(i, "1");
					i++;
					list.Insert(i, "");
					i++;
					list.Insert(i, "");
					i++;
					list.Insert(i, "");
					if (!dicItems.ContainsKey(element.Id))
					{
						dicItems.Add(element.Id, list);
					}
				}
				transaction.Start();
				viewPlan.GetUnderLayParameter().Set(elementId);
				transaction.Commit();
			}
		}

		private void GetAllWindowInSelArea(ref Dictionary<ElementId, List<string>> dicItems)
		{
			ProgressBarForm progressBarForm = new ProgressBarForm(this.m_lstSelElems.Count, "统计区域中窗的信息", ProgressBarStyle.Blocks, true, false);
			progressBarForm.Show(WindowHelper.HWndRevit);
			foreach (Element element in this.m_lstSelElems)
			{
				progressBarForm.RefreshProgressBar();
				if (progressBarForm.m_stopTag)
				{
					break;
				}
				if (element.Category.Id.IntegerValue == -2000014)
				{
					if (dicItems.ContainsKey(element.Id))
					{
						if (dicItems[element.Id][5] == "")
						{
							dicItems[element.Id][5] = "0";
						}
						dicItems[element.Id][5] = (Convert.ToInt32(dicItems[element.Id][5]) + 1).ToString();
					}
					else
					{
						FamilyInstance familyInstance = element as FamilyInstance;
						List<string> list = new List<string>();
						double elevation = element.GetYJKLevel().Elevation;
						list.Insert(0, elevation.ToString("0.00"));
						list.Insert(1, "材质窗");
						string name = familyInstance.Symbol.Name;
						list.Insert(2, name);
						string item = familyInstance.Symbol.GetParameter(BuiltInParameter.CASEWORK_WIDTH).AsValueString();
						list.Insert(3, item);
						string item2 = familyInstance.Symbol.GetParameter(BuiltInParameter.FAMILY_HEIGHT_PARAM).AsValueString();
						list.Insert(4, item2);
						int i;
						for (i = 0; i < this.m_lstLevelValue.Count; i++)
						{
							if (this.m_lstLevelValue[i].Equals(elevation))
							{
								list.Insert(5 + i, "1");
							}
							else
							{
								list.Insert(5 + i, "");
							}
						}
						i += 5;
						list.Insert(i, "1");
						i++;
						list.Insert(i, "");
						i++;
						list.Insert(i, "");
						i++;
						list.Insert(i, "");
						dicItems.Add(element.Id, list);
					}
				}
			}
		}

		private void GetAllWindowInLinkDoc(ref Dictionary<ElementId, List<string>> dicItems)
		{
			List<RevitLinkInstance> allRevitLinkInstancesInDoc = this.GetAllRevitLinkInstancesInDoc();
			if (allRevitLinkInstancesInDoc.Count <= 0)
			{
				return;
			}
			List<int> list = new List<int>();
			List<Dictionary<ElementId, List<string>>> list2 = new List<Dictionary<ElementId, List<string>>>();
			ProgressBarForm progressBarForm = new ProgressBarForm(this.m_lstViewPlan.Count, "统计链接文档中窗的信息", ProgressBarStyle.Blocks, true, false);
			progressBarForm.Show(WindowHelper.HWndRevit);
			foreach (ViewPlan viewPlan in this.m_lstViewPlan)
			{
				progressBarForm.RefreshProgressBar();
				if (progressBarForm.m_stopTag)
				{
					return;
				}
				double elevation = viewPlan.GenLevel.Elevation;
				int item = (int)(elevation * 1000.0);
				if (!list.Contains(item))
				{
					list.Add(item);
					double num = this.GetNearTopElevation(elevation);
					num -= 0.1;
					BoundingBoxUV outline = viewPlan.Outline;
					UV uv = outline.Min * (double)viewPlan.Scale;
					UV uv2 = outline.Max * (double)viewPlan.Scale;
					BoundingBoxIntersectsFilter input = new BoundingBoxIntersectsFilter(new Outline(new XYZ(uv.U, uv.V, elevation), new XYZ(uv2.U, uv2.V, num)));
					foreach (RevitLinkInstance revitLinkInstance in allRevitLinkInstancesInDoc)
					{
						Document linkDocument = revitLinkInstance.GetLinkDocument();
						if (linkDocument != null)
						{
							Dictionary<ElementId, List<string>> dictionary = new Dictionary<ElementId, List<string>>();
							try
							{
								FilteredElementCollectorLinkDocument filteredElementCollectorLinkDocument = new FilteredElementCollectorLinkDocument(linkDocument);
								filteredElementCollectorLinkDocument.AddViewLimit(input);
								foreach (BaseLinkDocumentResultBean baseLinkDocumentResultBean in filteredElementCollectorLinkDocument.OfCalss(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Windows).ToBeans())
								{
									Element useElement = baseLinkDocumentResultBean.UseElement;
									if (!dictionary.ContainsKey(useElement.Id))
									{
										FamilyInstance familyInstance = useElement as FamilyInstance;
										List<string> list3 = new List<string>();
										list3.Insert(0, elevation.ToString("0.00"));
										list3.Insert(1, "材质窗");
										string name = familyInstance.Symbol.Name;
										list3.Insert(2, name);
										string item2 = familyInstance.Symbol.GetParameter(BuiltInParameter.CASEWORK_WIDTH).AsValueString();
										list3.Insert(3, item2);
										string item3 = familyInstance.Symbol.GetParameter(BuiltInParameter.FAMILY_HEIGHT_PARAM).AsValueString();
										list3.Insert(4, item3);
										int i;
										for (i = 0; i < this.m_lstLevelValue.Count; i++)
										{
											if (this.m_lstLevelValue[i].Equals(elevation))
											{
												list3.Insert(5 + i, "1");
											}
											else
											{
												list3.Insert(5 + i, "");
											}
										}
										i += 5;
										list3.Insert(i, "1");
										i++;
										list3.Insert(i, "");
										i++;
										list3.Insert(i, "");
										i++;
										list3.Insert(i, "");
										list3.Add(linkDocument.PathName);
										dictionary.Add(useElement.Id, list3);
									}
								}
							}
							catch
							{
								continue;
							}
							list2.Add(dictionary);
						}
					}
				}
			}
			this.ManagerSameIdElem(list2, ref dicItems);
		}

		private List<RevitLinkInstance> GetAllRevitLinkInstancesInDoc()
		{
			IEnumerable<ElementId> enumerable = new FilteredElementCollector(this.m_doc).OfClass(typeof(RevitLinkInstance)).ToElementIds();
			List<RevitLinkInstance> list = new List<RevitLinkInstance>();
			foreach (ElementId elementId in enumerable)
			{
				Element element = this.m_doc.GetElement(elementId);
				if (!(element.GetType() != typeof(RevitLinkInstance)))
				{
					RevitLinkInstance revitLinkInstance = element as RevitLinkInstance;
					if (revitLinkInstance != null)
					{
						list.Add(revitLinkInstance);
					}
				}
			}
			return list;
		}

		private void SumData(ref Dictionary<ElementId, List<string>> dicItems)
		{
			if (dicItems.Count <= 0)
			{
				return;
			}
			foreach (ElementId elementId in new List<ElementId>(dicItems.Keys))
			{
				if (dicItems.ContainsKey(elementId))
				{
					List<string> list = dicItems[elementId];
					foreach (ElementId elementId2 in new List<ElementId>(dicItems.Keys))
					{
						if (dicItems.ContainsKey(elementId2) && !(elementId == elementId2))
						{
							List<string> list2 = dicItems[elementId2];
							if (list[2] == list2[2])
							{
								for (int i = 0; i < this.m_lstLevelValue.Count; i++)
								{
									int index = 5 + i;
									string text = list[index];
									string text2 = list2[index];
									if (text == "")
									{
										text = "0";
									}
									if (text2 == "")
									{
										text2 = "0";
									}
									list[index] = (Convert.ToInt32(text) + Convert.ToInt32(text2)).ToString();
									if (list[index] == "0")
									{
										list[index] = "";
									}
								}
								dicItems.Remove(elementId2);
							}
						}
					}
				}
			}
			List<int> list3 = new List<int>();
			if (this.m_standardLevel.Count == 0)
			{
				this.GetStandardLevel();
			}
			if (this.m_standardLevel.Count >= 1)
			{
				foreach (ElementId key in new List<ElementId>(dicItems.Keys))
				{
					int item = 0;
					List<string> list4 = dicItems[key];
					for (int j = 0; j < this.m_lstLevelValue.Count; j++)
					{
						string text3 = list4[5 + j];
						if (!(text3 == ""))
						{
							item = this.m_standardLevel[j].Count;
							list4[5 + j] = Convert.ToInt32(text3).ToString();
						}
					}
					list3.Add(item);
				}
			}
			int num = 0;
			foreach (ElementId key2 in new List<ElementId>(dicItems.Keys))
			{
				int num2 = 0;
				int num3 = 0;
				foreach (double num4 in this.m_lstLevelValue)
				{
					string text4 = dicItems[key2][5 + num3];
					if (text4 != "")
					{
						num2 += Convert.ToInt32(text4);
					}
					num3++;
				}
				if (num2 == 0)
				{
					dicItems[key2][5 + this.m_lstLevelValue.Count] = "";
				}
				else
				{
					dicItems[key2][5 + this.m_lstLevelValue.Count] = (num2 * list3[num]).ToString();
				}
				string key3 = dicItems[key2][1];
				if (this.m_dicData.ContainsKey(key3))
				{
					this.m_dicData[key3].Add(dicItems[key2]);
				}
				else
				{
					List<List<string>> list5 = new List<List<string>>();
					list5.Add(dicItems[key2]);
					this.m_dicData.Add(key3, list5);
				}
				num++;
			}
		}

		private List<double> GetLevelValueName()
		{
			List<double> list = new List<double>();
			foreach (ViewPlan viewPlan in this.m_lstViewPlan)
			{
				if (!list.Contains(viewPlan.GenLevel.Elevation))
				{
					list.Add(viewPlan.GenLevel.Elevation);
					this.m_lstLevelNames.Add(viewPlan.GenLevel.Name);
				}
			}
			list.Sort();
			return list;
		}

		private List<double> GetStandardLevelValueName()
		{
			List<double> list = new List<double>();
			for (int i = 0; i < this.m_lstViewPlan.Count; i++)
			{
				ViewPlan viewPlan = this.m_lstViewPlan[i];
				string item = "";
				ElementId standardLevelId = this.getStandardLevelId(viewPlan.GenLevel, ref item);
				if (!(standardLevelId == ElementId.InvalidElementId))
				{
					if (!standardLevelId.Equals(viewPlan.GenLevel.Id))
					{
						this.m_lstViewPlan.Remove(viewPlan);
						i--;
					}
					Level level = this.m_doc.GetElementById(standardLevelId) as Level;
					if (!list.Contains(level.Elevation))
					{
						list.Add(level.Elevation);
						this.m_lstLevelNames.Add(item);
					}
				}
			}
			list.Sort();
			return list;
		}

		private ElementId getStandardLevelId(Level lv, ref string standardlevelname)
		{
			foreach (StandardLevel standardLevel in this.m_standardLevel)
			{
				foreach (ElementId obj in standardLevel.LevelsId)
				{
					if (lv.Id.Equals(obj))
					{
						standardlevelname = standardLevel.LevelName;
						return standardLevel.StandardId;
					}
				}
			}
			return ElementId.InvalidElementId;
		}

		private double GetNearTopElevation(double dCurElevation)
		{
			for (int i = 0; i < this.m_lstLevelValue.Count - 1; i++)
			{
				double val = this.m_lstLevelValue[i];
				double num = this.m_lstLevelValue[i + 1];
				if (Geometry.LessThan(dCurElevation, num) && Geometry.Greaterthan_Or_Equal(dCurElevation, val))
				{
					return num;
				}
			}
			return dCurElevation + Common.MMToFeet(3000.0);
		}

		private bool SetDataToGrid()
		{
			if (this.m_dicData.Count <= 0 || this.m_grid == null)
			{
				YJKMessageBox.Show("没有需要统计的数据");
				return false;
			}
			this.m_grid.Cell(1, 1).Text = this.m_gridSet.GridName;
			this.m_grid.Cell(1, 1).FontSize = this.m_gridSet.TitleFontSize;
			this.m_grid.Cell(1, 1).FontName = this.m_gridSet.FontName;
			int num = 3;
			this.m_grid.Range(num + 1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).DeleteByRow();
			int num2 = 0;
			foreach (KeyValuePair<string, List<List<string>>> keyValuePair in this.m_dicData)
			{
				List<List<string>> value = keyValuePair.Value;
				num2 += value.Count;
			}
			if (num2 == 0)
			{
				YJKMessageBox.Show("没有找到统计数据");
				return false;
			}
			this.m_grid.Rows = num + num2 + 1;
			short width = this.m_grid.Column(5).Width;
			this.m_grid.Column(5).Delete();
			this.m_grid.Column(5).Delete();
			this.m_grid.InsertCol(5, this.m_lstLevelNames.Count);
			int num3 = 0;
			foreach (string text in this.m_lstLevelNames)
			{
				this.m_grid.Cell(3, 5 + num3).Text = this.m_lstLevelNames[num3];
				this.m_grid.Cell(3, 5 + num3).Alignment = AlignmentEnum.CenterCenter;
				this.m_grid.Column(5 + num3).Width = width;
				num3++;
			}
			this.m_grid.Range(2, 5, 2, 5 + num3 - 1).Merge();
			this.m_grid.Cell(2, 5).Text = "各层樘数";
			this.m_grid.Cell(2, 5).Alignment = AlignmentEnum.CenterCenter;
			foreach (KeyValuePair<string, List<List<string>>> keyValuePair2 in this.m_dicData)
			{
				List<List<string>> value2 = keyValuePair2.Value;
				if (value2.Count > 0)
				{
					int firstRow = num + 1;
					for (int i = 0; i < value2.Count; i++)
					{
						num++;
						for (int j = 1; j < value2[i].Count - 1; j++)
						{
							this.m_grid.Cell(num, j).Text = value2[i][j];
							this.m_grid.Cell(num, j).Alignment = AlignmentEnum.CenterCenter;
						}
					}
					this.m_grid.Range(firstRow, 1, num, 1).Merge();
					this.m_grid.Range(firstRow, 2, num, this.m_grid.Cols - 1).Sort(2, SortTypeEnum.Text, SortEnum.Ascending);
				}
			}
			this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontName = this.m_gridSet.FontName;
			this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontSize = this.m_gridSet.FontSize;
			if (this.m_gridSet.SetColWidth)
			{
				this.m_grid.Range(4, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).WrapText = true;
			}
			else
			{
				this.m_grid.Range(4, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).WrapText = false;
			}
			for (int k = 1; k < this.m_grid.Rows; k++)
			{
				this.m_grid.Row(k).AutoFit();
			}
			if (!this.m_gridSet.SetColWidth)
			{
				for (int l = 1; l < this.m_grid.Cols; l++)
				{
					this.m_grid.Column(l).AutoFit();
				}
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Outside, LineStyleEnum.Thick);
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Inside, LineStyleEnum.Thin);
			return true;
		}

        private FlexCell.Grid m_grid;

		private Document m_doc;

		private GridSetCommonInfo m_gridSet;

		private Dictionary<string, List<List<string>>> m_dicData = new Dictionary<string, List<List<string>>>();

		private List<ViewPlan> m_lstViewPlan;

		private List<Element> m_lstSelElems;

		private List<string> m_lstLevelNames = new List<string>();

		private List<double> m_lstLevelValue;

		private List<StandardLevel> m_standardLevel = new List<StandardLevel>();
	}
}
